package com.txzt.gaea.business.device.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.device.domain.model.DeviceInfo;
import com.txzt.gaea.business.device.service.IDeviceInfoService;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device/info")
public class DeviceInfoController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DeviceInfoController.class);

    @Autowired
    private IDeviceInfoService deviceInfoService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param deviceInfo
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, DeviceInfo deviceInfo) {
        logger.info(deviceInfo.toString());
        PageQuery<DeviceInfo> pageList = deviceInfoService.pageQuery(new PageQuery<>(pageNumber, pageSize, deviceInfo));
        return RtnInfo.success(pageList);
    }

    /**
     * 查询设备
     * @param deviceInfo
     * @return
     */
    @GetMapping(value = "/listDevice")
    public RtnInfo<?> listDevice(DeviceInfo deviceInfo) {
        return RtnInfo.success(deviceInfoService.selectDeviceInfoList(deviceInfo));
    }

    /**
     * 新增
     * @param deviceInfo
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody DeviceInfo deviceInfo) {
        deviceInfo.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        int count = deviceInfoService.addDeviceInfo(deviceInfo);
        if (98 == count) {
            return RtnInfo.error(98,"设备编号已存在！新增失败");
        }else if (99 == count){
            return RtnInfo.error(99,"同类型设备已存在！新增失败");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 编辑
     * @param deviceInfo
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody DeviceInfo deviceInfo) {
        int count = deviceInfoService.editDeviceInfo(deviceInfo);
        if (98 == count) {
            return RtnInfo.error(98,"设备编号已存在！编辑失败");
        }else if (99 == count){
            return RtnInfo.error(99,"同类型设备已存在！编辑失败");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setId(id);
        deviceInfo.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = deviceInfoService.updateById(deviceInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}
